package com.glory.hzlionpda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.bean.SpcBean;

import java.util.List;


public class SpcDataListAdapter extends BaseQuickAdapter<SpcBean, BaseViewHolder> {

    public SpcDataListAdapter(@Nullable List<SpcBean> data) {
        super(R.layout.adapter_spc_data_list, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, final SpcBean item) {
        helper.setText(R.id.tvName, item.getDVNAME()+"_"+item.getUNIT()+"_"+item.getDESC());
        EditText editText = helper.getView(R.id.etContent);
        setEditListener(editText, item);
    }

    private void setEditListener(EditText editText, final SpcBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setVALUE(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
